const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin"); // 自动生成 html 文件
const { CleanWebpackPlugin } = require("clean-webpack-plugin"); // 自动清理输出目录
const MiniCssExtractPlugin = require("mini-css-extract-plugin"); // 输出 css（如果不使用，css默认会被导入到js文件中）
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin'); // 压缩css插件

module.exports = {
  mode: "development",
  entry: {
    main: "./src/index.js"
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "[name].[hash:8].js"
  },
  devServer: {
    host: "localhost",
    port: 3030,
    open: true,
    hot: true
  },
  plugins: [
    new CleanWebpackPlugin(),
    // https://github.com/jantimon/html-webpack-plugin
    new HtmlWebpackPlugin({
      title: "广告测试",
      template: "./public/index.html",
      filename: "index.html",
      inject: true,
      minify: {
        collapseWhitespace: true, // 压缩空格
        removeAttributeQuotes: true, // 移除引号
        removeComments: true // 移除注释
      }
    }),
    new MiniCssExtractPlugin({
      filename: "index.[contenthash:8].css"
    }),
    new OptimizeCssAssetsPlugin(),
    new webpack.HotModuleReplacementPlugin() // 引入热更新插件
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, "css-loader"]
      },
      {
        test: /\.js$/,
        use: [
          {
            loader: "babel-loader",
            options: {
              presets: ["@babel/env"]
            }
          }
        ],
        include: path.resolve(__dirname, "src/")
      }
    ]
  }
};
